﻿HmJS.$Import('log');
HmJS.$Import('core.function');

HmJS.register('demo.module.types.functionmoo', function ($) {
	var that = {};

	this.hmID = 1;
	this.hmName = 'win-mark';

	// Changes the scope of this within the target function to refer to the bind parameter
	// 将函数的this指针绑定到某个对象上，也可以预先绑定部分参数
	// 比如 func.bind(obj,2,3)(4)就相当于 func.apply(obj,[2,3,4]);
	// 
	// Syntax: myFunction.bind([bind[, args]]);
	// 
	// Arguments: 
	//	bind - (object, 可选) The object that the "this" of the function will refer to.(函数中"this"引用的对象)
	//	args - (mixed, 可选) The arguments to pass to the function (must be an array if passing more than one argument).(传递给函数的参数(如果是多个参数,必须使用数组))
	//					这个注释应该是错误的，多个参数不能使用数组
	// 
	// Returns: 
	//	(function) The bound function.(重新绑定后的函数)
	that.bind = function () {
		log.info('function bind()---->')
		var testObj = {
			hmID: 2,
			hmName: 'obj-aron'
		}

		var myFunction = function (id, name) {
			// 当前'this'引用的对象是window对象
			this.hmID += id;
			this.hmName += name;
			log.info(this.hmID);
			log.info(this.hmName);
		}

		myFunction(1, '-bind');

		var fun1 = myFunction.bind(testObj);
		fun1(1, '-aaa');
		var fun2 = myFunction.bind(testObj, 1);
		fun2('-bbb');
		var fun3 = myFunction.bind(testObj, 1, '-ccc');
		fun3();
	}

	// Returns a closure with arguments and bind(返回指定了携带参数和绑定this对象的闭包函数)
	// bind的简化版两者都可以预绑参数和this对象. 区别就是这个函数注重预绑参数，bind注重预绑this对象。
	// 并且pass方法所返回的闭包函数无法连接绑定的参数和输入的参数,
	// 
	// Syntax: var newFunction = myFunction.pass([args[, bind]]);
	// 
	// Arguments: 
	//	args - (mixed, 可选) The arguments to pass to the function (must be an array if passing more than one argument).(传递给闭包函数的参数(如果是多个参数,必须使用数组))
	//	bind - (object, 可选) The object that the "this" of the function will refer to.(闭包函数中"this"引用的对象)
	// 
	// Returns: 
	//	(function) The function whose arguments are passed when called.(生成的闭包函数)
	that.pass = function () {
		log.info('function pass()---->')
		var testObj = {
			hmID: 2,
			hmName: 'obj-aron'
		}

		var myFunction = function (id, name) {
			// 当前'this'引用的对象是window对象
			this.hmID += id;
			this.hmName += name;
			log.info(this.hmID);
			log.info(this.hmName);
		}

		myFunction(1, '-pass');

		var fun1 = myFunction.pass([1, '-aaa'], testObj);
		fun1();
	}

	// Delays the execution of a function by a specified duration
	// 
	// Syntax: var timeoutID = myFunction.delay(delay[, bind[, args]]);
	// 
	// Arguments: 
	//	delay - (number) The duration to wait (in milliseconds).(延迟毫秒数)
	//	bind - (object, optional) The object that the "this" of the function will refer to.(函数对象中"this"引用的对象)
	//	args - (mixed, optional) The arguments passed (must be an array if the arguments are greater than one).(传递给函数的参数(如果是多个参数,必须使用数组))
	// 
	// Returns: 
	//	(number) The JavaScript timeout id (for clearing delays)(定时器引用(用于清除这个定时器时使用))
	that.delay = function () {
		log.info('function delay()---->')
		var testObj = {
			hmID: 2,
			hmName: 'obj-aron'
		}

		var myFunction = function (id, name) {
			// 当前'this'引用的对象是window对象
			this.hmID += id;
			this.hmName += name;
			log.info(this.hmID);
			log.info(this.hmName);
		}

		myFunction.delay(500, window, [1, '-delay']);

		var timer1 = myFunction.delay(1000, testObj, [1, '-aaa']);

		var timer2 = myFunction.delay(50);
		clearTimeout(timer2);
	}

	// Executes a function in the specified intervals of time. Periodic execution can be stopped using the clearInterval function
	// 
	// Syntax: var intervalID = myFunction.periodical(period[, bind[, args]]);
	// 
	// Arguments: 
	//	period - (number) The duration of the intervals between executions.(间隔毫秒数)
	//	bind - (object, optional) The object that the "this" of the function will refer to.(函数对象中"this"所引用的对象)
	//	args - (mixed, optional) The arguments passed (must be an array if the arguments are greater than one).(传递给闭包的参数(如果是多个参数,必须使用数组))
	// 
	// Returns: 
	//	(number) The Interval id (for clearing a periodical)(定时器引用(用于清除这个定时器时使用))
	that.periodical = function () {
		log.info('function periodical()---->')
		var testObj = {
			hmID: 2,
			hmName: 'obj-aron'
		}

		var timer1;

		var myFunction = function (id, name) {
			// 当前'this'引用的对象是window对象
			this.hmID += id;
			this.hmName += name;

			if (this.hmID > 4) {
				clearInterval(timer1);
			}

			log.info(this.hmID);
			log.info(this.hmName);
		}

		timer1 = myFunction.periodical(1000, testObj, [1, '-aaa']);
	}

	return that;
});